package com.nbicc.catalog.controller;

import com.nbicc.catalog.service.ICatalogService;
import com.nbicc.catalog.po.Cat;
import com.nbicc.common.ResponseObj;
import com.nbicc.common.TreeNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(value = "目录" , description = "目录操作")
@RestController
@RequestMapping(value = "/cat")
public class CatalogController {

    @Resource(name = "CatalogServiceImpl")
    private ICatalogService catalogService;

    @ApiOperation(value = "按id查询目录")
    @GetMapping("/id/{id}")
    @ResponseBody
    public ResponseObj selectById(@PathVariable int id) throws Exception {
        Cat cat = catalogService.selectById(id);
        return ResponseObj.success().setResult(cat);
    }

    @ApiOperation(value = "按id查询子目录" , notes = "根目录id为0")
    @GetMapping("/pid/{pid}")
    @ResponseBody
    public ResponseObj listChildrenByPid(@PathVariable int pid){
        List<Cat> cats = catalogService.listChild(pid);
        return ResponseObj.success().setResult(cats);
    }

    @ApiOperation(value = "按id查询子目录(树状)" , notes = "根目录id为0")
    @GetMapping("/tree/pid/{pid}")
    @ResponseBody
    public ResponseObj listChildrenByPidInTree(@PathVariable int pid){
        List<TreeNode> treeNodes = catalogService.listChildrenByPidInTree(pid);
        return ResponseObj.success().setResult(treeNodes);
    }

    @ApiOperation(value = "添加目录", notes = "目录id为系统生")
    @PostMapping
    @ResponseBody
    public ResponseObj insert(@RequestBody Cat cat) throws Exception{
        catalogService.insert(cat);
        return ResponseObj.success().setResult(cat);
    }

    @ApiOperation(value = "目录信息修改", notes = "必须上传需要修改的目录id")
    @PutMapping
    @ResponseBody
    public ResponseObj update(@RequestBody Map<String,Object> cat) throws Exception{
        catalogService.updateById(cat);
        return ResponseObj.success();
    }

    @ApiOperation(value = "删除目录", notes = "只需包含根目录id")
    @DeleteMapping
    @ResponseBody
    public ResponseObj delete(@RequestBody Cat cat){
        catalogService.deleteById(cat.getId());
        return ResponseObj.success();
    }

    @ApiOperation(value = "转换为链接目录", notes = "该目录直接引用一个新闻")
    @PutMapping("/transLink")
    @ResponseBody
    public ResponseObj transLink(@RequestBody Cat cat) throws Exception{
        catalogService.transLink(cat);
        return ResponseObj.success();
    }

    @ApiOperation(value = "转换为普通目录", notes = "该目录下是一个新闻列表信息")
    @PutMapping("/transCat")
    @ResponseBody
    public ResponseObj transCat(@RequestBody Cat cat) throws Exception{
        catalogService.transCat(cat);
        return ResponseObj.success();
    }

    @ApiOperation(value = "查询新闻所在的目录")
    @GetMapping("/newsId/{newsId}")
    @ResponseBody
    public ResponseObj selectByNewsId(@PathVariable long newsId) {
        List<Cat> catList = catalogService.selectByNewsId(newsId);
        return ResponseObj.success().setResult(catList);
    }

}
